<template>
  <div class="lunbo-container">
    <!--  不可能加两个字段-->

    <div class="toolbar">
      <el-button type="primary" size="small" class="add-btn" @click="handleAdd">
        新增
      </el-button>
      <el-button type="danger" size="small" class="batch-delete-btn" :disabled="multipleSelection.length === 0"
        @click="handleBatchDelete">
        批量删除
      </el-button>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="鉴定专区轮播图" name="1">
        <el-table v-loading="loading" :data="arr1" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" />
          <el-table-column align="center" prop="id" label="图片id" />
          <el-table-column align="center" prop="href" label="跳转页面地址" />
          <el-table-column prop="imgUrl" label="图片" align="center">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 60px" :src="scope.row.imgUrl"
                :preview-src-list="[scope.row.imgUrl]" fit="cover">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" class="edit-btn"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" class="delete-btn"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="大主页Banner图" name="3">
        <el-table v-loading="loading" :data="arr3" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" />
          <el-table-column align="center" prop="name" label="名称" />
          <el-table-column align="center" prop="id" label="图片id" />
          <el-table-column align="center" prop="href" label="跳转页面地址" />
          <el-table-column prop="imgUrl" label="图片" align="center">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 60px" :src="scope.row.imgUrl"
                :preview-src-list="[scope.row.imgUrl]" fit="cover">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="语言" align="center" prop="language" />
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" class="edit-btn"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" class="delete-btn"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="挂售Banner图" name="4">
        <el-table v-loading="loading" :data="arr4" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" />
          <el-table-column align="center" prop="name" label="名称" />

          <el-table-column align="center" prop="id" label="图片id" />
          <el-table-column align="center" prop="href" label="跳转页面地址" />
          <el-table-column prop="imgUrl" label="图片" align="center">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 60px" :src="scope.row.imgUrl"
                :preview-src-list="[scope.row.imgUrl]" fit="cover">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="语言" align="center" prop="language" />
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" class="edit-btn"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" class="delete-btn"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="竞拍Banner图" name="5">
        <el-table v-loading="loading" :data="arr5" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" />
          <el-table-column align="center" prop="name" label="名称" />

          <el-table-column align="center" prop="id" label="图片id" />
          <el-table-column align="center" prop="href" label="跳转页面地址" />
          <el-table-column prop="imgUrl" label="图片" align="center">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 60px" :src="scope.row.imgUrl"
                :preview-src-list="[scope.row.imgUrl]" fit="cover">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="语言" align="center" prop="language" />
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" class="edit-btn"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" class="delete-btn"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <!-- <el-tab-pane label="商品轮播图" name="second">
        <el-table v-loading="loading" :data="arr2" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" />
          <el-table-column align="center" prop="id" label="图片id" />

          <el-table-column align="center" prop="href" label="跳转页面地址" />
          <el-table-column prop="type" label="类型" align="center">
            <template slot-scope="scope">
              {{ scope.row.type == 1 ? '首页轮播图' : '商品轮播图' }}
            </template>
          </el-table-column>

          <el-table-column prop="imgUrl" label="图片" align="center">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 60px" :src="scope.row.imgUrl"
                :preview-src-list="[scope.row.imgUrl]" fit="cover">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" type="primary" icon="el-icon-edit" class="edit-btn"
                @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" icon="el-icon-delete" class="delete-btn"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane> -->
    </el-tabs>

    <!-- 添加轮播图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <!-- 类型 -->
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option label="鉴定专区轮播图" :value="1" />
            <!-- <el-option label="商品轮播图" :value="2" /> -->
            <el-option label="大主页Banner图" :value="3" />
            <el-option label="挂售主页Banner图" :value="4" />
            <el-option label="竞拍主页Banner图" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-select v-model="form.language" placeholder="请选择语言">
            <el-option label="中文" value="zh-cn" />
            <el-option label="中文繁体" value="zh-tw" />
            <el-option label="日文" value="ja-jp" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false"
            :on-success="handleUploadSuccess" :before-upload="beforeUpload">
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转页面地址" prop="href">
          <el-input v-model="form.href" placeholder="请输入跳转页面地址" />
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLunbo, deleteLunbo, addLunbo } from '@/api/system/lunbo'

export default {
  name: 'Lunbo',
  data() {
    return {
      activeName: '1', // 当前激活的标签页
      // 遮罩层
      loading: false,
      // 轮播图表格数据
      lunboList: [],
      arr1: [],
      arr2: [],
      arr3: [],
      arr4: [],
      arr5: [],
      // 多选数据
      multipleSelection: [],
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {
        imgUrl: undefined,
        type: 1
      },
      // 表单校验
      rules: {
        imgUrl: [
          { required: true, message: "请上传图片", trigger: "change" }
        ]
      },
      // 上传地址
      uploadUrl: process.env.VUE_APP_BASE_API + "/file/s3/upload"
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(tab, event) {
      this.activeName = tab.name;
      this.getList();
    },
    /** 查询轮播图列表 */
    getList() {
      this.loading = true
      listLunbo({
        type: this.activeName
      }).then(response => {
        this.lunboList = response.data
        this.arr1 = this.lunboList.filter(item => item.type === 1)
        this.arr2 = this.lunboList.filter(item => item.type === 2)
        this.arr3 = this.lunboList.filter(item => item.type === 3)
        this.arr4 = this.lunboList.filter(item => item.type === 4)
        this.arr5 = this.lunboList.filter(item => item.type === 5)
        this.loading = false
      })
    },
    /** 表格多选变化 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 批量删除按钮操作 */
    handleBatchDelete() {
      const ids = this.multipleSelection.map(item => item.id)
      this.$confirm('确定要删除选中的数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        deleteLunbo(ids).then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // TODO: 实现修改功能
      this.form = JSON.parse(JSON.stringify(row));

      this.open = true;
      this.title = "修改轮播图";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = [row.id];
      this.$confirm('确定要删除该条数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        deleteLunbo(ids).then(() => {
          this.$message.success('删除成功');
          this.getList();
        });
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加轮播图";
    },
    // 表单重置
    reset() {
      this.form = {
        imgUrl: undefined,
        type: Number(this.activeName),
      };
      this.resetForm("form");
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const data = {
            imgUrl: this.form.imgUrl,
            type: this.form.type,
            href: this.form.href,
            id: this.form.id
          };
          if (this.form.language) {
            data.language = this.form.language
          }
          if (this.form.name) {
            data.name = this.form.name
          }
          addLunbo(data).then(response => {
            this.$message.success("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 文件上传成功处理
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.form.imgUrl = res.data.url;
        // this.$message.success("上传成功");
      } else {
        this.$message.error("上传失败");
      }
    },
    // 上传前图片格式校验
    beforeUpload(file) {
      const isImage = file.type.indexOf("image/") === 0;
      const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isImage) {
      //     this.$message.error("上传文件只能是图片格式!");
      //     return false;
      // }
      // if (!isLt2M) {
      //     this.$message.error("上传图片大小不能超过 2MB!");
      //     return false;
      // }
      return true;
    }
  }
}
</script>

<style scoped>
.lunbo-container {
  padding: 24px 16px 0 16px;
  background: #fafbfc;
}

.toolbar {
  display: flex;
  align-items: center;
  margin-bottom: 14px;
  gap: 10px;
}

.add-btn,
.batch-delete-btn {
  border-radius: 4px;
  font-weight: normal;
  box-shadow: none;
  transition: box-shadow 0.2s, background 0.2s;
  padding: 6px 22px;
  font-size: 15px;
}

.add-btn:hover {
  background: #1890ff;
  color: #fff;
}

.batch-delete-btn:hover {
  background: #ff4d4f;
  color: #fff;
}

.edit-btn {
  border-radius: 4px;
  font-weight: normal;
  box-shadow: none;
  transition: box-shadow 0.2s, background 0.2s;
  padding: 6px 18px;
  font-size: 15px;
}

.edit-btn:hover {
  background: #1890ff;
  color: #fff;
}

.delete-btn {
  border-radius: 4px;
  font-weight: normal;
  box-shadow: none;
  transition: box-shadow 0.2s, background 0.2s;
  padding: 6px 18px;
  font-size: 15px;
}

.delete-btn:hover {
  background: #ff4d4f;
  color: #fff;
}

.avatar-uploader .el-upload {
  border: 2px dashed #409EFF;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
  background: #f0f7ff;
  width: 360px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-uploader-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  background: #fff;
  border: 2px solid #409EFF;
  border-radius: 12px;
  font-size: 48px;
  color: #409EFF;
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.08);
}

.avatar-uploader .el-upload:hover .avatar-uploader-icon {
  border-color: #66b1ff;
  color: #66b1ff;
  box-shadow: 0 4px 16px rgba(64, 158, 255, 0.15);
  background: #f6fbff;
}

.avatar {
  width: 200px;
  height: 100%;
  display: block;
  object-fit: cover;
  border-radius: 6px;
}
</style>
